/**
 * Project 1 COSC 671 Spring 2010
 * @author Hui Liu
 * @dueDate Feb. 14th, 2010
 * @purpose
 * The Stutter.c program takes a filename as an argument and then scans the words (character by character) to report any repeated (i.e., stuttered) words.
 * 1. Change the program from C to Java
 * 2. Format the program according to the Java Programming Style Guidelines and the Tips for Maintainable Java Code discussed and handed out in class
 * 3. Document your Java code in Javadoc format and generate the Javadoc API HTML files for your code
 *
*/
package task4;

import java.io.FileReader;
import java.util.Scanner;

// TODO: Auto-generated Javadoc
/**
 * The Class NewStutter contains methods to scans the words (word by word)
 * from input source to report any repeated (i.e., stuttered) words..
 * <p>
 * The Class NewStutter use java.util.Scanner to scan the words word by word other than character by character to simplify the program
 * 
 * @author Hui Liu
 * @see Stutter
 */
public class NewStutter {

	/**
	 * The main method takes filename as arguments and use the method stut of Class NewStutter to
	 * scans the words (word by word) to report any repeated (i.e., stuttered) words.
	 * <p>
	 * If there is no arguments, the program read data from the standard input device; 
	 * otherwise the arguments is a list of input filenames, the program will read the file one by one to scan for repeated words.
	 * 
	 * @param args 	command-line arguments, a list of filenames to be read
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in;
		NewStutter stutter = new NewStutter();
		
		if (args.length == 0) {
			// read data from the standard input device
			in = new Scanner(System.in);
			stutter.stut(in);
		} else {
			// read file one by one
			for (int i = 0; i < args.length; i++) {
				try {
					// read data from the file specified in the argument
					in = new Scanner(new FileReader(args[i]));
					stutter.stut(in);
					in.close();
				} catch (Exception e) {
					// failed to read the file, print error message
					System.out.println("stutter error: " + e.toString());
				}
			}

		}

	}



	/**
	 * Scans the words (word by word) from the input source to 
	 * report any repeated (i.e., stuttered) words by printing out "Repeated word on line %d: %s %s\n"
	 * 
	 * <p><b>Algorithm:</b></p>
	 * <pre>
	 * while not the end of the source file {
	 * 		read a line
	 * 		increase the line number
	 * 
	 * 		while not the end of the line {
	 * 			read a word from the line
	 * 			compare current word with previous word
	 * 			if they are the same
	 * 					print out
	 * 			else 
	 * 					copy it to previous word
	 * 		}
	 * } end of while
	 * </pre>
	 * 	 
	 * @param in the input source
	 *
	*/
	public void stut(Scanner in) {
		String currentWord = new String();
		String previousWord = new String();
		int lineNo = 0;
		Scanner line;

		// while not the end of file
		while (in.hasNext()) {
			line = new Scanner(in.nextLine());	// read a line from file
			lineNo++;
			line.useDelimiter(("[\\s,.!-+=;:?&{}\\ ]+"));	// set the delimiters to split the words
			
			// while not the end of the line
			while (line.hasNext()) {
				currentWord = line.next();	// read next word from the line to currentWord
				// if current word is the same as previous word, report
				if (currentWord.compareTo(previousWord) == 0)
					System.out.printf("Repeated word on line %d: %s %s\n",
							lineNo, previousWord, currentWord);
				else
					previousWord = currentWord; // point previousWord to currentWord
			}
		}
		System.out.println();
	}

}
